package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("travel")
public class TravelController {
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;

    @RequestMapping("releaseList")
    public String list(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        qo.setState(Travel.STATE_RELEASE);
        model.addAttribute("pageInfo", travelService.queryForList(qo));
        return "travel/releaseTravel";
    }
    @RequestMapping("waitList")
    public String waitList(@ModelAttribute("qo") TravelQueryObject qo, Model model) {
        model.addAttribute("pageInfo", travelService.queryForList(qo));
        return "travel/waitTravel";
    }

    @RequestMapping("getContent")
    @ResponseBody
    public JsonResult getContent(Long id) {
        JsonResult result = new JsonResult();
        try {
            TravelContent content = travelService.getContent(id);
            result.setResult(content);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("updateState")
    @ResponseBody
    public JsonResult updateState(Long id,int state) {
        JsonResult result = new JsonResult();
        try {
            travelService.updateState(id,state);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @RequestMapping("listTravels")
    public String listTravels(@ModelAttribute("qo") StrategyCatalogQueryObject qo, Model model){
        model.addAttribute("strategies",strategyService.listAll());
        model.addAttribute("pageInfo",travelService.selectTravelByStrategyId(qo));
        model.addAttribute("travels",travelService.selectAllReaseTravel());
        return "/strategy/strategyAndTravel";
    }

    @RequestMapping("deleteTravelId")
    @ResponseBody
    public JsonResult deleteTravelId(Long travelId){
        travelService.deleteTravelId(travelId);
        return new JsonResult();
    }

    @RequestMapping("save")
    public String save(Long strategyId,Long travelId){
        travelService.saveStrategyAndTravel(strategyId,travelId);
        return "redirect:/travel/listTravels.do";
    }
}
